package com.zggk.iroad.util;

import android.app.Activity;
import android.content.Context;
import android.util.Base64;
import android.util.DisplayMetrics;

import com.blankj.utilcode.util.StringUtils;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.Calendar;

public class MyUtils {
    private static int x;                  // 日期属性：年
    private static int y;                  // 日期属性：月
    private static int z;                  // 日期属性：日
    private static Calendar localTime = Calendar.getInstance();    // 当前日期
    public static boolean isNull(String str) {
        if (str != null) {
            str = str.trim();
        }
        if (str == null || "".equals(str)) {
            return true;
        }
        return false;
    }
    public static int getWidth2(Activity activity) {
        DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenWidth = dm.widthPixels/5*2;
        return screenWidth;
    }
    /**
     * dip转pix
     *
     * @param context
     * @param dp
     * @return
     */
    public static int dp2px(Context context, float dp) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }
    public static String thisMonth() {
        String strY = null;
        x = localTime.get(Calendar.YEAR);
        y = localTime.get(Calendar.MONTH) + 1;
        strY = y >= 10 ? String.valueOf(y) : ("0" + y);
        return x + "-" + strY + "-01";
    }
    public static String thisMonthEnd() {
        String strY = null;
        String strZ = null;
        boolean leap = false;
        x = localTime.get(Calendar.YEAR);
        y = localTime.get(Calendar.MONTH) + 1;
        if (y == 1 || y == 3 || y == 5 || y == 7 || y == 8 || y == 10 || y == 12) {
            strZ = "31";
        }
        if (y == 4 || y == 6 || y == 9 || y == 11) {
            strZ = "30";
        }
        if (y == 2) {
            leap = leapYear(x);
            if (leap) {
                strZ = "29";
            } else {
                strZ = "28";
            }
        }
        strY = y >= 10 ? String.valueOf(y) : ("0" + y);
        return x + "-" + strY + "-" + strZ;
    }
    public static boolean leapYear(int year) {
        boolean leap;
        if (year % 4 == 0) {
            if (year % 100 == 0) {
                if (year % 400 == 0) leap = true;
                else leap = false;
            } else leap = true;
        } else leap = false;
        return leap;
    }
    public static int getWidth3(Activity activity) {
        DisplayMetrics dm = new DisplayMetrics();
        activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
        int screenWidth = dm.widthPixels/6;
        return MyUtils.dp2px(activity,screenWidth);
    }
    public static String bmpToBase64String(String imgPath) {

        // 用io流读取到要上传的图片，用Base64编码成字节流的字符串,得到uploadBuffer（要传到webservice接口上）
        FileInputStream fis = null;
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        String uploadBuffer = "";
        try {
            if (StringUtils.isEmpty(imgPath)) {
                return "";
            }
            fis = new FileInputStream(imgPath);
            byte[] buffer = new byte[16];
            int count = -1;
            while ((count = fis.read(buffer)) >= 0) {
                baos.write(buffer, 0, count);
            }
            // 进行Base64编码得到字符串
            uploadBuffer = Base64.encodeToString(baos.toByteArray(), Base64.NO_WRAP);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return uploadBuffer;
    }
    public static String getZHMCByZH(String zhStr) {
        String result = "";
        if (!isNull(zhStr)) {
            if (zhStr.contains(".")) {
                String zhfStr = zhStr.substring(0, zhStr.indexOf("."));
                String zhaStr = zhStr.substring(zhStr.indexOf(".") + 1, zhStr.length());
                if (zhaStr.length() == 1) {
                    zhaStr += "00";
                } else if (zhaStr.length() == 2) {
                    zhaStr += "0";
                }
                result = "K" + zhfStr + "+" + zhaStr;
            } else {
                result = "K" + zhStr;
            }
        }
        return result;
    }
}
